<?php
/* 
* @Author: LDF QQ 47121862
* @Date:   2014-05-29 15:26:36
* @Last Modified by:   LDF QQ 47121862
* @Last Modified time: 2014-06-05 13:52:27
*/
Class CartControl extends Control{
	public function __init(){
		// 购物车初始化
		if(!isset($_SESSION['cart'])){
			$_SESSION['cart']=array();
			$_SESSION['cart']['goods']=array();
			$_SESSION['cart']['total']=0;
			$_SESSION['cart']['prices']=0;
		}
		
	}
	//显示所有购物
	public function index(){
		// p($_SESSION);
		$this->seeCartGoods();
		$this->display('index.html');
	}
	//根据session查找购物车商品并分配给goods到模板
	private function seeCartGoods(){
		$goods='';
		if(!empty($_SESSION['cart']['goods'])){
			$cart=$_SESSION['cart']['goods'];
			$stid=array_keys($cart);//查找所有商品库存id
			// 取得所有商品
			$sql="SELECT * FROM hd_g_goods AS g JOIN hd_g_stock AS s 
			ON g.gid=s.gid WHERE s.stid IN(".implode(',', $stid).")";
			$goods=M()->query($sql);
			//增加属性和商品返回地址
			foreach ($goods as $k => $v) {
				$goods[$k]['_attr']=$_SESSION['cart']['goods'][$v['stid']]['avalue'];
				$goods[$k]['_url']=$_SESSION['cart']['goods'][$v['stid']]['url'];
			}
		}
		
		// p($goods);
		$this->goods=$goods;
	}
	//添加到购物车
	public function add(){
		$buy=Q('buyNum',0,'intval'); //购买数量
		$stid=Q('stid'); //库存id
		$avalue=Q('avalue'); //商品属性
		$url=U("Index/content",Q('url')); //商品链接地址
		//检测库存
		$stock=M('stock')->find($stid);
		if(!$stock) $this->error('商品不存在',__ROOT__);
		// 判断用户是否点击同款产品
		if(isset($_SESSION['cart']['goods'][$stid])){
			$_SESSION['cart']['goods'][$stid]['num']+=$buy;
		}else{
			$_SESSION['cart']['goods'][$stid]=array(
				'num'=>$buy,
				'price'=>$stock['price'],
				'url'=>$url,
				'avalue'=>$avalue
			);
		}
		$total=0;$prices=0;
		foreach ($_SESSION['cart']['goods'] as $v) {
			$total+=$v['num'];
			$prices+=$v['num']*$v['price'];
		}
		$_SESSION['cart']['total']=$total;
		$_SESSION['cart']['prices']=$prices;
		$this->display('add.html');
	}
	//删除购物车
	public function del(){
		$stid=Q('stid',0,'intval');
		$_SESSION['cart']['total']-=$_SESSION['cart']['goods'][$stid]['num'];
		$_SESSION['cart']['prices']-=$_SESSION['cart']['goods'][$stid]['price']*$_SESSION['cart']['goods'][$stid]['num'];
		unset($_SESSION['cart']['goods'][$stid]);
		go('index');
	}

	//显示订单页面
	function dingdan(){
		// p($_SESSION);
		//查找出所有订购商品
		$this->seeCartGoods();
		//查找当前用户收货地址
		$this->dizhi=M('shop_address')->where('uid='.$_SESSION['uid']['uid'])->all();
		$this->display();
	}
	//收货地址ajax
	function address(){
		$phone=Q('phone');
		$sheng=Q('sheng');
		$shi=Q('shi');
		$xian=Q('xian');
		$xiangxi=Q('xiangxi');
		$name=Q('name');
		if(empty($phone) || empty($name) || empty($sheng) || empty($shi) || empty($xian) || empty($xiangxi)){
			echo 0;die;
		}
		$_POST['address']=$sheng.$shi.$xian.$xiangxi;
		$_POST['uid']=$_SESSION['uid']['uid'];
		$_POST['def']=1;
		//插入之前将所有默认地址都清除
		$def=array('def'=>0);
		M('shop_address')->where(array('uid'=>$_SESSION['uid']['uid']))->save($def);
		//插入新地址
		$id=M('shop_address')->add($_POST);
		$addressNew=M('shop_address')->find($id);
		$_SESSION['address']=$addressNew;
		echo 1;die;
	}
	//改变收货地址时
	function changeAddress(){
		$said=Q('said',0,'intval');
		//清空所有默认地址
		$def=array('def'=>0);
		M('shop_address')->where(array('uid'=>$_SESSION['uid']['uid']))->save($def);
		// 修改默认地址
		M('shop_address')->where('said='.$said)->save(array('def'=>1));
		//修改SESSION
		$addressNew=M('shop_address')->find($said);
		$_SESSION['address']=$addressNew;
		echo json_encode($addressNew);die;
	}
	//订单最终提交
	function dingdanOk(){
		if(empty($_SESSION['cart']['goods'])){
			GO(__ROOT__);
		}
		//组合订单需要的表单
		$data=$this->orderData();
		$oid=M('order')->add($data);
		if($oid){
			foreach ($_SESSION['cart']['goods'] as $k => $v) {
				$listData=array(
					'oid'=>$oid,
					'stid'=>$k,
					'num'=>$v['num'],
					'price'=>$v['price']
				);
				M('order_list')->add($listData);
			}
		}
		//清空购物车
		unset($_SESSION['cart']);
		GO(U('shouyintai'));
	}
	public function shouyintai(){
		// $dingdan=M('order')->where('uid='.$_SESSION['uid']['uid'])->all();
		// p($dingdan);
		$this->display('shouyintai.html');
	}
	//组合订单需要的表单
	private function orderData(){
		$re = array(
			'uid'=>$_SESSION['uid']['uid'],
			'consignee'=>$_SESSION['address']['name'],
			'prices'=>$_SESSION['cart']['prices'],
			'phone'=>$_SESSION['address']['phone'],
			'tel'=>$_SESSION['address']['tel'],
			'address'=>$_SESSION['address']['address'],
			'order_time'=>time(),
			'ordersn'=>Cart::getOrderId()
		);
		return $re;
	}
}
